package priv.ln.communication.exer;

/**
 * ClassName: Clerk
 * Package: priv.ln.communication.exer
 * Description:
 * 共享数据类
 * @Author 辣辣
 * @Create 2024/7/21 15:30
 * @Version 1.0
 */
public class Clerk {
    private int productNum = 0;//产品的数量

    //增加产品数量的方法
    public synchronized void addProduct(){

        if(productNum >= 20){
            //等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            productNum++;
            System.out.println(Thread.currentThread().getName() + "生产了第" + productNum + "个产品");

            //唤醒
            notifyAll();
        }

    }

    //减少产品数量的方法
    public synchronized void minusProduct(){

        if(productNum <= 0){

            //等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println(Thread.currentThread().getName() + "消费了第" + productNum + "个产品");
            productNum--;

            //唤醒
            notifyAll();
        }

    }
}
